package com.ruoyi.ocean.controller;

import java.util.List;
import java.util.Arrays;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ocean.domain.vo.OceanLogTotalVo;
import com.ruoyi.ocean.domain.bo.OceanLogTotalBo;
import com.ruoyi.ocean.service.IOceanLogTotalService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 操作日志统计
 *
 * @author linziheng
 * @date 2024-05-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/ocean/logTotal")
public class OceanLogTotalController extends BaseController {

    private final IOceanLogTotalService iOceanLogTotalService;
    @Autowired
    private MongoTemplate mongoTemplate;
    @GetMapping("/getMonthInit")
    public List<String> getMonthInit() {
        return iOceanLogTotalService.getMonthInit();
    }
    /**
     * 查询操作日志统计列表
     */
//    @SaCheckPermission("ocean:logTotal:list")
//    @GetMapping("/list")
//    public TableDataInfo<OceanLogTotalVo> list(OceanLogTotalBo bo, PageQuery pageQuery) {
//        return iOceanLogTotalService.queryPageList(bo, pageQuery);
//    }
    @SaCheckPermission("ocean:logTotal:list")
    @GetMapping("/list")
    public PageImpl<OceanLogTotalVo> list(OceanLogTotalBo bo, PageQuery pageQuery) {
        int page = pageQuery.getPageNum();
        int size = pageQuery.getPageSize();
        int skip = (page - 1) * size;
        DBObject obj = new BasicDBObject();
        //查询条件
        if(!Objects.isNull(bo.getAccountId())){
            obj.put("accountId", new BasicDBObject("$eq", bo.getAccountId()));
        }
        if(!Objects.isNull(bo.getMonth())){
            obj.put("month", new BasicDBObject("$eq", bo.getMonth()));
        }
//        obj.put("id", new BasicDBObject("$eq", "100121"));
//
        BasicDBObject fieldsObject = new BasicDBObject();
        //展示列
        fieldsObject.put("id", 1);
        fieldsObject.put("accountId", 1);
        fieldsObject.put("total", 1);
        fieldsObject.put("dTotal", 1);
        fieldsObject.put("qTotal", 1);
        fieldsObject.put("isOk", 1);
        fieldsObject.put("month", 1);
        fieldsObject.put("createTime", 1);
        // 创建查询对象
        BasicQuery query = new BasicQuery(obj.toString(), fieldsObject.toString()); // 这里使用空JSON对象查询所有文档
        query.skip(skip); // 设置跳过的文档数
        query.limit(size); // 设置返回的文档数上限

        // 执行查询
        List<OceanLogTotalVo> content = mongoTemplate.find(query, OceanLogTotalVo.class);

        // 获取总文档数（可以进一步优化为只查询一次）
        long total = mongoTemplate.count(new BasicQuery("{}"), OceanLogTotalVo.class);

        // 创建分页对象
        PageRequest pageRequest = PageRequest.of(page, size);
        return new PageImpl<>(content, pageRequest, total);
    }



    @SaCheckPermission("ocean:logTotal:list")
    @GetMapping("/listQC")
    public TableDataInfo<OceanLogTotalVo> listQC(OceanLogTotalBo bo, PageQuery pageQuery) {
        return iOceanLogTotalService.queryPageListQC(bo, pageQuery);
    }
    /**
     * 导出操作日志统计列表
     */
    @SaCheckPermission("ocean:logTotal:export")
    @Log(title = "操作日志统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OceanLogTotalBo bo, HttpServletResponse response) {
        List<OceanLogTotalVo> list = iOceanLogTotalService.queryNoPageListQC(bo);
        ExcelUtil.exportExcel(list, "操作日志统计", OceanLogTotalVo.class, response);
    }

    @SaCheckPermission("ocean:logTotal:export")
    @Log(title = "操作日志统计", businessType = BusinessType.EXPORT)
    @PostMapping("/exportAD")
    public void exportAD(OceanLogTotalBo bo, HttpServletResponse response) {
        List<OceanLogTotalVo> list = mongoTemplate.find(new BasicQuery("{}"), OceanLogTotalVo.class);
        ExcelUtil.exportExcel(list, "操作日志统计", OceanLogTotalVo.class, response);
    }

    /**
     * 获取操作日志统计详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("ocean:logTotal:query")
    @GetMapping("/{id}")
    public R<OceanLogTotalVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iOceanLogTotalService.queryById(id));
    }

    /**
     * 新增操作日志统计
     */
    @SaCheckPermission("ocean:logTotal:add")
    @Log(title = "操作日志统计", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody OceanLogTotalBo bo) {
        return toAjax(iOceanLogTotalService.insertByBo(bo));
    }

    /**
     * 修改操作日志统计
     */
    @SaCheckPermission("ocean:logTotal:edit")
    @Log(title = "操作日志统计", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody OceanLogTotalBo bo) {
        return toAjax(iOceanLogTotalService.updateByBo(bo));
    }

    /**
     * 删除操作日志统计
     *
     * @param ids 主键串
     */
    @SaCheckPermission("ocean:logTotal:remove")
    @Log(title = "操作日志统计", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iOceanLogTotalService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
